package cn.itsource.pet.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.jwt.JwtUtil;
import cn.itsource.basic.jwt.Payload;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.service.IPetService;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public AjaxResult patchGrounding(Long[] ids) {
        for (Long id : ids) {
            Pet pet = petMapper.findOne(id);
            if (pet.getState() == 0) {
                pet.setState(1);
                pet.setOnsaletime(new Date());
                petMapper.update(pet);
            } else {
                throw new BusinessException("你选择得宠物中有已上架的宠物，请重新选择！！！");
            }
        }
        return new AjaxResult();
    }

    @Transactional
    @Override
    public AjaxResult offShelf(Long[] ids) {
        for (Long id : ids) {
            Pet pet = petMapper.findOne(id);
            if (pet.getState() == 1) {
                pet.setState(0);
                pet.setOffsaletime(new Date());
                petMapper.update(pet);
            } else {
                throw new BusinessException("你选择得宠物中有已下架的宠物，请重新选择！！！");
            }
        }
        return new AjaxResult();
    }


    @Override
    public void add(Pet pet, HttpServletRequest request) {
        pet.setOnsaletime(new Date());
        pet.setCreatetime(new Date());

        String token = request.getHeader("token");
        Payload payload = JwtUtil.parseJwtToken(token);
        Logininfo logininfo = payload.getLogininfo();
        User user = userMapper.findByLogininfoId(logininfo.getId());
        pet.setUserId(user.getId());
        petMapper.add(pet);

        //批量保存宠物图片t_image表
        if (pet.getImgUrls() != null && pet.getImgUrls().size() > 0){
            String url = "";
            for (String imgUrl : pet.getImgUrls()) {
                    url =url + imgUrl + ",";
            }
            String stringUrl = url.substring(0, url.length() - 1);
            petMapper.batchUpdate(stringUrl,pet.getId());
        }
        PetDetail petDetail = new PetDetail();
        petDetail.setPetId(pet.getId());
        petDetail.setAdoptNotice(pet.getAdoptNotice()); //领养须知
        petDetail.setIntro(pet.getIntro()); //简介
        petDetailMapper.add(petDetail);
    }

    @Override
    public String getResources(Long id) {
        return petMapper.getResources(id);
    }

    @Override
    public void update(Pet pet) {
        PetDetail petDetail = petDetailMapper.findByPetId(pet.getId());
        petDetail.setPetId(pet.getId());
        petDetail.setAdoptNotice(pet.getAdoptNotice());
        petDetail.setIntro(pet.getIntro());
        petDetailMapper.update(petDetail);
        super.update(pet);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        Pet pet = petMapper.findOne(id);
        petDetailMapper.deleteByPetId(pet.getId());
        super.delete(id);
    }

    @Transactional
    @Override
    public void patchdel(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
    }


}
